package com.lcj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcj.entity.Users;
import com.lcj.mapper.ProfileMapper;
import com.lcj.mapper.UserMapper;
import com.lcj.service.IProfileService;
import com.lcj.utils.UserUtils;
import com.lcj.vo.Profile;
import com.lcj.vo.Profiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProfileService implements IProfileService {
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Profiles getProfiles(String userName) {
        Profiles profiles = new Profiles();
        Profile profile = new Profile();
        List<Profile> profileList = profileMapper.getProfile(userName);
        if (profileList.size() == 0) {
            throw new RuntimeException("用户不存在");
        } else {
            Users loginUser = UserUtils.getLoginUser();
            Long id = loginUser.getId();

            for (int i = 0; i < profileList.size(); i++) {
                if (profileList.get(i).getFollowId() == null) {
                    profile.setFollowing(false);
                    break;
                }
                if (profileList.get(i).getFollowId().equals(id)) {
                    profile.setFollowing(true);
                    break;
                } else {
                    if (i == profileList.size() - 1)
                        profile.setFollowing(false);
                }

            }
            profile.setUsername(profileList.get(0).getUsername());
            profile.setBio(profileList.get(0).getBio());
            profile.setImage(profileList.get(0).getImage());
            profiles.setProfile(profile);
            return profiles;
        }
    }

    @Override
    public Profiles follow(String userName) {
        Long id = UserUtils.getLoginUser().getId();
        Profiles profiles1 = getProfiles(userName);
        if (profiles1.getProfile().isFollowing()) {
            throw new RuntimeException("已经关注了");
        }
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userName);
        Users user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        if (user.getId().equals(UserUtils.getLoginUserId())) {
            throw new RuntimeException("不能关注自己");
        }
        profileMapper.follow(userName, id);
        Profiles profiles = getProfiles(userName);
        return profiles;
    }

    @Override
    public Profiles unfollow(String userName) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userName);
        Users user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        Profiles profiles1 = getProfiles(userName);
        if (!profiles1.getProfile().isFollowing()) {
            throw new RuntimeException("未关注");
        }
        if (user.getId().equals(UserUtils.getLoginUserId())) {
            throw new RuntimeException("不能取消关注自己");
        }
        Long id = UserUtils.getLoginUser().getId();
        profileMapper.unfollow(userName, id);
        Profiles profiles = getProfiles(userName);
        return profiles;
    }
}
